/*
 * game.c
 *
 *  Created on: 15 nov. 2010
 *      Author: Nicolas Aucouturier, Laurent R�veill�re
 */

#include <assert.h>
#include <time.h>

#include "../include/game.h"
#include "../include/map.h"
#include "../include/player.h"
#include "../include/misc.h"
#include "../include/level.h"
#include "../include/bomb.h"
#include "../include/list_bomb.h"
#include "../include/monster.h"

int nb_monster = 3;

struct s_listbomb {
	t_bomb bomb;
	t_listbomb next;
};

struct s_game {
	t_level curr_level; // current level
	t_player player;
	//t_monster monster[10];
	t_monster monster;
	int monster1;
	t_listbomb listbomb;
	int bomb;

	//t-monster monster;

	SDL_Surface * number[10];
	SDL_Surface * banner_life;
	SDL_Surface * banner_bomb;
	SDL_Surface * banner_range;
	SDL_Surface * banner_line;

};

static struct s_game the_game;

t_game game_new(void) {
	srand(time(NULL ));

	the_game.curr_level = level_get_level(0); // get maps of the first level

	the_game.player = player_init(5,3); // FIXME

	the_game.monster = monster_init();
	player_from_map(the_game.player, level_get_map(the_game.curr_level, 0)); // get x,y of the player on the first map
	monster_from_map(the_game.monster, level_get_map(the_game.curr_level, 0));
	//the_game.bomb= bomb_init();

	/* load banner's imgs in memory */
	the_game.number[0] = load_image(IMG_BANNER_0);
	the_game.number[1] = load_image(IMG_BANNER_1);
	the_game.number[2] = load_image(IMG_BANNER_2);
	the_game.number[3] = load_image(IMG_BANNER_3);
	the_game.number[4] = load_image(IMG_BANNER_4);
	the_game.number[5] = load_image(IMG_BANNER_5);
	the_game.number[6] = load_image(IMG_BANNER_6);
	the_game.number[7] = load_image(IMG_BANNER_7);
	the_game.number[8] = load_image(IMG_BANNER_8);
	the_game.number[9] = load_image(IMG_BANNER_9);
	the_game.banner_life = load_image(IMG_BANNER_LIFE);
	the_game.banner_bomb = load_image(IMG_BANNER_BOMB);
	the_game.banner_range = load_image(IMG_BANNER_RANGE);
	the_game.banner_line = load_image(IMG_BANNER_LINE);

	// Creates an empty bomb list
	the_game.listbomb = list_new();

	return &the_game;
}

void game_free(t_game game) {
	assert(game);

	player_free(game->player);
	level_free(game->curr_level);
}

t_player game_get_player(t_game game) {
	assert(game);
	return game->player;
}


/*
list game_listbomb(t_game game) {  //Return the player listbomb
	assert(game);
	return game->listbomb;
}
*/

void game_banner_display(t_game game, SDL_Surface* screen) {
	assert(game);
	assert(screen);

	t_map map = level_get_curr_map(game_get_curr_level(game));

	SDL_Rect place;

	int i;
	place.y = (map_get_height(map)) * SIZE_BLOC;
	for (i = 0; i < map_get_width(map); i++) {
		place.x = i * SIZE_BLOC;
		SDL_BlitSurface(game->banner_line, NULL, screen, &place);
	}

	place.y = (map_get_height(map) * SIZE_BLOC) + SIZE_LINE;

	int white_bloc = ((map_get_width(map) * SIZE_BLOC) - 6 * SIZE_BLOC) / 4;


	// CHANGE THE NUMBER OF LIFE
	place.x = white_bloc;
	SDL_BlitSurface(game->banner_life, NULL, screen, &place);
	place.x = white_bloc + SIZE_BLOC;

		if (player_get_nb_life(game_get_player(game)) >= 0
					&& player_get_nb_life(game_get_player(game)) < 10)
				SDL_BlitSurface(game->number[player_get_nb_life(game_get_player(game))],
						NULL, screen, &place);


	//SDL_BlitSurface(game->number[2], NULL, screen, &place);

	place.x = 2 * white_bloc + 2 * SIZE_BLOC;
	SDL_BlitSurface(game->banner_bomb, NULL, screen, &place);
	place.x = 2 * white_bloc + 3 * SIZE_BLOC;
	;
	if (player_get_nb_bomb(game_get_player(game)) >= 0
			&& player_get_nb_bomb(game_get_player(game)) < 10)
		SDL_BlitSurface(game->number[player_get_nb_bomb(game_get_player(game))],
				NULL, screen, &place);

	place.x = 3 * white_bloc + 4 * SIZE_BLOC;
	SDL_BlitSurface(game->banner_range, NULL, screen, &place);
	place.x = 3 * white_bloc + 5 * SIZE_BLOC;
	// TODO : link bomb_range in banner and bomb_range of player
	int bomb_range = 1;
	SDL_BlitSurface(game->number[bomb_range], NULL, screen, &place);
}

void game_display(t_game game,SDL_Surface* screen) {
	assert(game);
	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));

	game_banner_display(game, screen);
	level_display(game_get_curr_level(game), screen);

	t_listbomb list_save = game->listbomb; // save

	while(game->listbomb != NULL && game->listbomb->bomb != NULL) {
		bomb_display(game->listbomb->bomb, screen,game->player); //Display the bomb

		game->listbomb = game->listbomb->next;
	}

	game->listbomb = list_save; // restore

	player_display(game->player, screen);

	if(game->monster1==1)
	monster_display(game->monster, screen);

	//monster_display(monster_get_listmonster(), screen);// ---------------------------- ICI POUR LES MONSTRES */


/*
	//monster_display(game->monster,screen);
	int i;
	for(i=0;i<nb_monster;i++){
			if (game->monster[i] != NULL){			// kill monster if 0 life
				monster_display(game->monster[i], screen);
			}
		}
*/



	SDL_Flip(screen); // SDL_Flip -- swaps screen buffers -- update
}

t_level game_get_curr_level(t_game game) {
	return game->curr_level;
}

////////MODIFICATION
uint input_update(t_game game) {


//uint input_update(t_game game, SDL_Surface *screen) {
	SDL_Event event;
	t_player player = game_get_player(game);
	//t_monster monster;
	t_map map = level_get_curr_map(game_get_curr_level(game));



	while (SDL_PollEvent(&event)) {
		switch (event.type) {
		case SDL_QUIT:
			return 1;
		case SDL_KEYDOWN:
			switch (event.key.keysym.sym) {
			case SDLK_ESCAPE:
				return 1;
			case SDLK_UP:
				player_set_current_way(player, NORTH);
				player_move(player, map);
				take_key(game->player,map); //Take the Key
				break;
			case SDLK_DOWN:
				player_set_current_way(player, SOUTH);
				player_move(player, map);
				take_key(game->player,map);//Take the Key
				break;
			case SDLK_RIGHT:
				player_set_current_way(player, EAST);
				player_move(player, map);
				take_key(game->player,map);//Take the Key
				break;
			case SDLK_LEFT:
				player_set_current_way(player, WEST);
				player_move(player, map);
				take_key(game->player,map);//Take the Key
				break;
			case SDLK_SPACE:
				// TODO Set a bomb
				//player_inc_nb_bomb(player);//Augmente le nb de bombes
				//player_set_current_way(player, WEST);
				//map_set_cell_type(map,player_get_x(player),player_get_y(player),CELL_BOMB);//Place une clé à la position (x-1,y)
				//player_move(player, map);
				//player_dec_nb_bomb(player);//Decrease the number of bomber

				//putBomb(map,player); //To put a bomb under the man


				//bomb_create (player,map,newlist);

				//bomb_create(player,map);
				//

				/*LAST
				 * bomber(player,map);
								 */
				if( player_get_nb_bomb(game->player) > 0)
				{

					//fprintf(stderr, "avant BOMB CREATE %p\n", game->listbomb);
					bomb_create(game, map, 1);

					bomb_explode (game->player,map);

					player_dec_nb_bomb(game->player);
					//take_key(game->player,map);

					fprintf(stderr, "avant BOMB CREATE %d\n", player_get_x(player));

					//fprintf(stderr, "avant BOMB CREATE %d\n", rand_a_b(0,5));
					//int i=1;
					//fprintf(stderr, "avant BOMB CREATE %d\n", i);
					//bomber(game->player,map);
				}
				else
					player_inc_nb_bomb(game->player);

/*
	*			bomber(player,map,game_listbomb(game));
*/
				//bombPrint(player,map,maListe);

				//Appuie touche espace marque commentaire fichier

/*
				fichier=fopen("bar.txt","w");


				fprintf(fichier,"hey");
				fclose(fichier);
*/
/*
				if( player_get_nb_bomb(game_get_player(game)) > 0 )
				{
				int x;
				int y;

				x=player_get_x(game->player);
				y=player_get_y(game->player);
				float one_second = (1000/30);
				float timer;
				timer=4;
				int power=2;

				game->bomb = set_bomb(x,y,timer * one_second,power);
				}
				*/
				break;


			case SDLK_F1:
				map_set_cell_type(map,player_get_x(player),player_get_y(player),CELL_STONE);
				break;


			case SDLK_F2:
			{
				//monster_move(monster, map);
				game->monster1=1;
				break;

			}
			default:
				break;
			}
			break;

		}
	}
	return 0;
}



/*
int game_update(t_game game, t_map map, t_listmonster listmonster) {


	SDL_Surface* screen; //Pour utiliser screen




	 if(input_update(game, screen, listmonster) == 1) {
		return 1; // exit game
	}

 	// TODO monsters management
	//DEBUT ------------------------------
	//int k;PAS BESOIN DE L UTILISER (pour le moment)

	//t_listmonster listmonster;

	listmonster = monster_get_listmonster();  //We get the static listmonster



	int x;		// x = random value
	srand(time(NULL));

//for (k = 0; k < game_time_get_cycles_to_calculate(); k++) {
	while(listmonster != NULL) {


		t_player monster;
		monster = monster_get_monster(listmonster);  //We get the monster inside the list

		//int mx = player_get_x(monster);
		//int my = player_get_y(monster);//////GUILLAUME
		//if (player_get_nb_life(monster) >= 0) {
		//if (game_time_ticks(0) - player_get_timer(monster) > 333) { //3 moves in a second

			x = 1 + rand () % 4;  //x is a random value between 1 and 4
		switch (x) {			  //We move the monster consequently in a direction
		case 1:
			player_set_current_way(monster, SOUTH);
			//player_move(monster, game_get_map(game), screen, 1, multi);
			player_move(monster, map);
			break;  //the 1 in the function indicates that we move a monster
		case 2:
			player_set_current_way(monster, EAST);
			//player_move(monster, game_get_map(game), screen, 1, multi);
			player_move(monster, map);
			break;

		case 3:
			player_set_current_way(monster, WEST);
			//player_move(monster, game_get_map(game), screen, 1, multi);
			player_move(monster, map);
			break;
		case 4:
			player_set_current_way(monster, NORTH);
			//player_move(monster, game_get_map(game), screen, 1, multi);
			player_move(monster, map);
			break;


		// Lives check ON N A PAS ENCORE FAIT LES VIES

		//if (player_get_x(monster) != mx || player_get_y(monster) != my) { //Only if the monster has moved

			if (player_get_x(monster) == player_get_x(game_get_player(game)) && player_get_y(monster) == player_get_y(game_get_player(game)))
				player_dec_nb_life(game_get_player(game), multi, screen);

			if (multi) {
				if (player_get_x(monster) == player_get_x(game_get_player2(game)) && player_get_y(monster) == player_get_y(game_get_player2(game)))
					player2_dec_nb_life(game_get_player2(game), screen);
			}
		}

		//The monster timer is set to the total time
		//player_set_timer(monster, game_time_ticks(0)); ON NE GERE PAS (encore !) LE TEMPS
		//} BOUCLE IF l. 156
		}

		//We check the next monster
		listmonster=monster_next(listmonster);
	//BOUCLE FOR l. 146 }

	//FIN ------------------------------
	}

	return EXIT_SUCCESS;
}
*/
	int game_update(t_game game) {
	        if(input_update(game) == 1) {
	                return 1; // exit game
	        }

	        // TODO monsters management ...

	        //t_player player = game_get_player(game);
	     // t_map map = level_get_curr_map(game_get_curr_level(game));
	        	//assert(game);
	        	//SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));


	        //if (bomb_damages(game->bomb, game->player, map) &&
				//player_get_nb_life(game->player)>0){		//do damages to player
				//player_dec_nb_life(game->player);
			//}



	         //t_listbomb list_save = game->listbomb; // save

	        //	while(game->listbomb != NULL && game->listbomb->bomb != NULL) {
	        	//	bomb_damages(game->listbomb->bomb,player,map);
//
	        	//	game->listbomb = game->listbomb->next;
	        	//}




	        		   //     	game->listbomb = list_save; // restore

	        return 0;
	}
